//
//  OAActionCell.m
//  OASDK_Works
//
//  Created by ChenJie on 4/4/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OAActionCell.h"
#import "OABase.h"

typedef NS_ENUM(NSUInteger, ComponentId) {
    Title = 1,
    Content,
    AddBeforeButton,
    AddAfterButton,
    DeleteButton,
    DeleteBeforeButton,
    DeleteAfterButton,
};

@interface OAActionCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *addBeforeButton;
@property (nonatomic, strong) UIButton *addAfterButton;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) UIButton *deleteBeforeButton;
@property (nonatomic, strong) UIButton *deleteAfterButton;

@end

@implementation OAActionCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.addBeforeButton];
        [self.contentView addSubview:self.addAfterButton];
        [self.contentView addSubview:self.deleteButton];
        [self.contentView addSubview:self.deleteBeforeButton];
        [self.contentView addSubview:self.deleteAfterButton];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.titleLabel.frame = [self.viewModel.oa_layouts[@(Title)] CGRectValue];
    self.contentLabel.frame = [self.viewModel.oa_layouts[@(Content)] CGRectValue];
    self.addBeforeButton.frame = [self.viewModel.oa_layouts[@(AddBeforeButton)] CGRectValue];
    self.addAfterButton.frame = [self.viewModel.oa_layouts[@(AddAfterButton)] CGRectValue];
    self.deleteBeforeButton.frame = [self.viewModel.oa_layouts[@(DeleteBeforeButton)] CGRectValue];
    self.deleteButton.frame = [self.viewModel.oa_layouts[@(DeleteButton)] CGRectValue];
    self.deleteAfterButton.frame = [self.viewModel.oa_layouts[@(DeleteAfterButton)] CGRectValue];
    
//    [self.contentView oa_showBounds];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
}

#pragma mark - Public methods

#define kTitleFont [UIFont boldSystemFontOfSize:16]
#define kContentFont [UIFont systemFontOfSize:16]
#define kButtonFont [UIFont systemFontOfSize:14]
#define kMarginH 15
#define kMarginV 10
#define kSpaceBetweenV 5
#define kButtonWidth 100
#define kButtonHeight 30
#define kButtonMarginTop 10
#define kSpaceBetweenButtonH 5
#define kSeparatorHeight 0.5

+ (CGFloat)oa_heightWithWidth:(CGFloat)cellWidth viewModel:(OAActionCellViewModel *)viewModel {
    NSMutableDictionary *componentFrames = [NSMutableDictionary dictionary];

    CGFloat topMark = 0;
    CGFloat leftMark = 0;
    CGFloat labelWidthMax = cellWidth - kMarginH * 2;
    
    topMark += kMarginV;
    CGSize titleSize = [viewModel.title oa_sizeWithWidth:labelWidthMax font:kTitleFont];
    CGRect titleFrame = CGRectMake(kMarginH, topMark, titleSize.width, titleSize.height);
    componentFrames[@(Title)] = OAValueWithRect(titleFrame);
    topMark += titleSize.height;
    
    topMark += kSpaceBetweenV;
    CGSize contentSize = [viewModel.content oa_sizeWithWidth:labelWidthMax font:kContentFont];
    CGRect contentFrame = CGRectMake(kMarginH, topMark, contentSize.width, contentSize.height);
    componentFrames[@(Content)] = OAValueWithRect(contentFrame);
    topMark += contentSize.height;
    
    topMark += kButtonMarginTop;
    leftMark = kMarginH;
    CGRect addBeforeButtonFrame = CGRectMake(leftMark, topMark, kButtonWidth, kButtonHeight);
    componentFrames[@(AddBeforeButton)] = OAValueWithRect(addBeforeButtonFrame);
    leftMark += kButtonWidth;
    
    leftMark += kSpaceBetweenButtonH;
    CGRect addAfterButtonFrame = CGRectMake(leftMark, topMark, kButtonWidth, kButtonHeight);
    componentFrames[@(AddAfterButton)] = OAValueWithRect(addAfterButtonFrame);
    topMark += kButtonHeight;
    
    topMark += kSpaceBetweenV;
    leftMark = kMarginH;
    CGRect deleteBeforeButtonFrame = CGRectMake(leftMark, topMark, kButtonWidth, kButtonHeight);
    componentFrames[@(DeleteBeforeButton)] = OAValueWithRect(deleteBeforeButtonFrame);
    leftMark += kButtonWidth;
    
    leftMark += kSpaceBetweenButtonH;
    CGRect deleteButtonFrame = CGRectMake(leftMark, topMark, kButtonWidth, kButtonHeight);
    componentFrames[@(DeleteButton)] = OAValueWithRect(deleteButtonFrame);
    leftMark += kButtonWidth;
    
    leftMark += kSpaceBetweenButtonH;
    CGRect deleteAfterButtonFrame = CGRectMake(leftMark, topMark, kButtonWidth, kButtonHeight);
    componentFrames[@(DeleteAfterButton)] = OAValueWithRect(deleteAfterButtonFrame);
    
    viewModel.oa_layouts = componentFrames;
    
     return CGRectGetMaxY(deleteAfterButtonFrame);
}

#pragma mark - Private methods



#pragma mark - Event response methods

- (void)addBeforeButtonDidClick:(UIButton *)button {
    self.viewModel.oa_executeActionWithEvent(@(OAActionCellEventAddBeforeButtonDidClick));
}

- (void)addAfterButtonDidClick:(UIButton *)button {
    self.viewModel.oa_executeActionWithEvent(@(OAActionCellEventAddAfterButtonDidClick));
}

- (void)deleteBeforeButtonDidClick:(UIButton *)button {
    self.viewModel.oa_executeActionWithEvent(@(OAActionCellEventDeleteBeforeButtonDidClick));
}

- (void)deleteButtonDidClick:(UIButton *)button {
    self.viewModel.oa_executeActionWithEvent(@(OAActionCellEventDeleteButtonDidClick));
}

- (void)deleteAfterButtonDidClick:(UIButton *)button {
    self.viewModel.oa_executeActionWithEvent(@(OAActionCellEventDeleteAfterButtonDidClick));
}

#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(OAActionCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.titleLabel.text = viewModel.title;
    self.contentLabel.text = viewModel.content;
}

#pragma mark - Getter

OAGetLabel(titleLabel, ^(UILabel *label){
    label.font = kTitleFont;
    label.numberOfLines = 0;
})

OAGetLabel(contentLabel, ^(UILabel *label){
    label.font = kContentFont;
    label.numberOfLines = 0;
})

OAGetButton(addBeforeButton, ^(UIButton *button){
    [button setTitle:@"Add Before" forState:UIControlStateNormal];
    button.titleLabel.font = kButtonFont;
    button.backgroundColor = OARandomColor();
})

OAGetButton(addAfterButton, ^(UIButton *button){
    [button setTitle:@"Add After" forState:UIControlStateNormal];
    button.titleLabel.font = kButtonFont;
    button.backgroundColor = OARandomColor();
})

OAGetButton(deleteButton, ^(UIButton *button){
    [button setTitle:@"Delete" forState:UIControlStateNormal];
    button.titleLabel.font = kButtonFont;
    button.backgroundColor = OARandomColor();
})

OAGetButton(deleteBeforeButton, ^(UIButton *button){
    [button setTitle:@"Delete Before" forState:UIControlStateNormal];
    button.titleLabel.font = kButtonFont;
    button.backgroundColor = OARandomColor();
})

OAGetButton(deleteAfterButton, ^(UIButton *button){
    [button setTitle:@"Delete After" forState:UIControlStateNormal];
    button.titleLabel.font = kButtonFont;
    button.backgroundColor = OARandomColor();
})

#pragma mark - Memory Management Methods

- (void)dealloc {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end
